package com.ruoyi.system.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.domain.Activity;
import com.ruoyi.system.domain.Signup;
import com.ruoyi.system.domain.vo.MailVo;
import com.ruoyi.system.domain.vo.SigninVo;
import com.ruoyi.system.service.IActivityService;
import com.ruoyi.system.service.ISignupService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.Notice;
import com.ruoyi.system.service.INoticeService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 通知功能Controller
 * 
 * @author hyb
 * @date 2023-02-17
 */
@RestController
@RequestMapping("/acnotice")
public class ActivityNoticeController extends BaseController
{
    @Autowired
    private INoticeService noticeService;

    @Autowired
    private IActivityService activityService;

    @Autowired
    private ISignupService signupService;

    @Autowired
    RabbitTemplate rabbitTemplate;

    /*
    * 查询通知对象
    * */
    @RequiresPermissions("notice:notice:listNoticeObject")
    @GetMapping("/listNoticeObject")
    public AjaxResult listNoticeObject(@RequestParam("id")String id){
        Activity activity = activityService.selectActivityById(Long.parseLong(id));
        if (activity==null){
            //如果不存在,就当做活动编号去查询一次
            Activity newA = new Activity();
            newA.setNumber(id);
            List<Activity> activities = activityService.selectActivityList(newA);
            if (activities.size()==0){
                return AjaxResult.error("该活动不存在");
            }else{
                activity=activities.get(0);
                id=String.valueOf(activity.getId());
            }
        }
        if (!activity.getUserId().equals(SecurityUtils.getUserId())){
            return AjaxResult.error("您非活动负责人,无法查询通知对象");
        }
        //查询该活动id下所有的报名者
        List<Signup> signinVoList=signupService.selectSignupByActivityId(id);
        return AjaxResult.success().put("list",signinVoList);
    }

    /**
     * 查询通知功能列表
     */
    @RequiresPermissions("notice:notice:list")
    @GetMapping("/list")
    public TableDataInfo list(Notice notice)
    {
        startPage();
        List<Notice> list = noticeService.selectNoticeList(notice);
        return getDataTable(list);
    }

    /**
     * 导出通知功能列表
     */
    @RequiresPermissions("notice:notice:export")
    @Log(title = "通知功能", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Notice notice)
    {
        List<Notice> list = noticeService.selectNoticeList(notice);
        ExcelUtil<Notice> util = new ExcelUtil<Notice>(Notice.class);
        util.exportExcel(response, list, "通知功能数据");
    }

    /**
     * 获取通知功能详细信息
     */
    @RequiresPermissions("notice:notice:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(noticeService.selectNoticeById(id));
    }

    /**
     * 新增通知功能
     */
    @RequiresPermissions("notice:notice:add")
    @Log(title = "通知功能", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Notice notice)
    {
        //只有自己是负责人才可以
        Activity activity = activityService.selectActivityById(notice.getActivityId());
        if (!activity.getUserId().equals(SecurityUtils.getUserId())){
            return AjaxResult.error("你非该活动负责人,无法添加通知");
        }
        //先查是否已经该活动的通知
        Notice n = new Notice();
        n.setActivityId(activity.getId());
        List<Notice> notices = noticeService.selectNoticeList(n);
        if (notices.size()!=0){
            return AjaxResult.error("该活动已添加通知,可前往通知管理里进行修改");
        }

        //不限定活动类型,但选择被通知人的时候只能选择该活动的报名人

        notice.setUserId(SecurityUtils.getUserId());
        notice.setUserName(SecurityUtils.getUsername());

        noticeService.insertNotice(notice);
        MailVo mailVo = new MailVo();
        mailVo.setContent(notice.getContent());
        mailVo.setTos(notice.getNoticeIds());
        mailVo.setFrequency(Integer.parseInt(String.valueOf(notice.getFrequency())));
        mailVo.setStartTime(notice.getStartTime());
        mailVo.setCount(Integer.parseInt(String.valueOf(notice.getPf1())));
        //发送消息,让mq去发送多个邮件
        rabbitTemplate.convertAndSend("topicExchange","topic.email.qqMail",mailVo);

        return AjaxResult.success();
    }

    /**
     * 修改通知功能
     */
    @RequiresPermissions("notice:notice:edit")
    @Log(title = "通知功能", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Notice notice)
    {
        return toAjax(noticeService.updateNotice(notice));
    }

    /**
     * 删除通知功能
     */
    @RequiresPermissions("notice:notice:remove")
    @Log(title = "通知功能", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(noticeService.deleteNoticeByIds(ids));
    }
}
